package cn._51doit.flink.day02;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;
import java.util.UUID;


public class ParallelCustomSource1 {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //可以认为设置执行环境的并行度
        //env.setParallelism(6);
        //System.out.println(env.getParallelism());

        DataStreamSource<Integer> lines = env.addSource(new ParallelMySource1());

        lines.print();

        env.execute();


    }

    private static class ParallelMySource1 implements ParallelSourceFunction<Integer> {


        private boolean flag = true;

        @Override
        public void run(SourceContext<Integer> ctx) throws Exception {
            System.out.println("RUN method Invoked !!!");
            Random random = new Random();
            while (flag) {
                int i = random.nextInt();
                ctx.collect(i);
                Thread.sleep(2000);
            }
        }

        @Override
        public void cancel() {
            System.out.println("CANCEL method Invoked @@@@");
            flag = false;
        }
    }


}
